using FineCollection.Intf;

using NUnit.Framework;



namespace FineCollection.Cont
{
    [TestFixture]
    public class BitIntSet_test
    {
        
        [Test]
        public void test_Basic_1()
        {
            BitIntSet bs = new BitIntSet();

            Assert.AreEqual(0, bs.Count);
            Assert.IsTrue(bs.IsEmpty);
            Assert.IsFalse(bs.IsNotEmpty);
            Assert.IsFalse(bs.Contains(3));
            Assert.IsFalse(bs.Contains(7));

            bs.Include(3);

            Assert.AreEqual(1, bs.Count);
            Assert.IsFalse(bs.IsEmpty);
            Assert.IsTrue(bs.IsNotEmpty);
            Assert.IsTrue(bs.Contains(3));
            Assert.IsFalse(bs.Contains(7));

            bs.Include(7);

            Assert.AreEqual(2, bs.Count);
            Assert.IsTrue(bs.Contains(3));
            Assert.IsTrue(bs.Contains(7));

            bs.Exclude(3);

            Assert.AreEqual(1, bs.Count);
            Assert.IsFalse(bs.Contains(3));
            Assert.IsTrue(bs.Contains(7));

            bs.Exclude(7);

            Assert.AreEqual(0, bs.Count);
            Assert.IsFalse(bs.Contains(3));
            Assert.IsFalse(bs.Contains(7));
        }


        [Test]
        public void test_InitiallyEmptySet()
        {
            BitIntSet bs = new BitIntSet();

            Assert.AreEqual(0, bs.Count);

            Assert.IsFalse(bs.Contains(33));
            Assert.IsFalse(bs.Contains(1007));
            Assert.IsFalse(bs.Contains(-5));
        }


        [Test]
        public void test_ExtendBits1()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(3);
            bs.Include(90);

            Assert.IsTrue(bs.Contains(3));
            Assert.IsTrue(bs.Contains(90));
            Assert.AreEqual(2, bs.Count);
        }


        [Test]
        public void test_ExtendBits2()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(3);
            bs.Include(1007);

            Assert.IsTrue(bs.Contains(3));
            Assert.IsTrue(bs.Contains(1007));
            Assert.AreEqual(2, bs.Count);
        }


        [Test] 
        public void test_Inlcude_CheckFirst_1()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(7);
            Assert.AreEqual(7, bs.First);

            bs.Include(17);
            Assert.AreEqual(7, bs.First);

            bs.Include(3);
            Assert.AreEqual(3, bs.First);
        }


        [Test] 
        public void test_Inlcude_CheckFirst_2()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(1001);
            Assert.AreEqual(1001, bs.First);

            bs.Include(3003);
            Assert.AreEqual(1001, bs.First);

            bs.Include(7);
            Assert.AreEqual(7, bs.First);
        }


        [Test] 
        public void test_Inlcude_CheckLast_1()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(7);
            Assert.AreEqual(7, bs.Last);

            bs.Include(3);
            Assert.AreEqual(7, bs.Last);

            bs.Include(17);
            Assert.AreEqual(17, bs.Last);
        }


        [Test] 
        public void test_Inlcude_CheckLast_2()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(1001);
            Assert.AreEqual(1001, bs.Last);

            bs.Include(15);
            Assert.AreEqual(1001, bs.Last);

            bs.Include(3003);
            Assert.AreEqual(3003, bs.Last);
        }


        [Test] 
        public void test_Exclude_CheckFirst_1()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(5);
            bs.Include(7);
            bs.Include(11);
            bs.Include(13);
            bs.Include(17);

            Assert.AreEqual(5, bs.First);

            bs.Exclude(7);
            Assert.AreEqual(5, bs.First);

            bs.Exclude(5);
            Assert.AreEqual(11, bs.First);
        }


        [Test] 
        public void test_Exclude_CheckFirst_2()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(1001);
            bs.Include(2001);
            bs.Include(3001);
            bs.Include(4001);
            bs.Include(5001);

            Assert.AreEqual(1001, bs.First);

            bs.Exclude(2001);
            Assert.AreEqual(1001, bs.First);

            bs.Exclude(1001);
            Assert.AreEqual(3001, bs.First);
        }


        [Test] 
        public void test_Exclude_CheckLast_1()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(5);
            bs.Include(7);
            bs.Include(11);
            bs.Include(13);
            bs.Include(17);

            Assert.AreEqual(17, bs.Last);

            bs.Exclude(13);
            Assert.AreEqual(17, bs.Last);

            bs.Exclude(17);
            Assert.AreEqual(11, bs.Last);
        }


        [Test] 
        public void test_Exclude_CheckLast_63()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(1);
            bs.Include(62);
            bs.Include(63);

            bs.Exclude(63);
            Assert.AreEqual(62, bs.Last);
        }


        [Test] 
        public void test_Exclude_CheckLast_64()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(1);
            bs.Include(63);
            bs.Include(64);

            bs.Exclude(64);
            Assert.AreEqual(63, bs.Last);
        }


        [Test] 
        public void test_Exclude_CheckFirstLast_FirstOfPair()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(11);
            bs.Include(17);

            bs.Exclude(11);
            Assert.AreEqual(17, bs.First);
            Assert.AreEqual(17, bs.Last);
        }


        [Test] 
        public void test_Exclude_CheckFirstLast_LastOfPair()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(11);
            bs.Include(17);

            bs.Exclude(17);
            Assert.AreEqual(11, bs.First);
            Assert.AreEqual(11, bs.Last);
        }



        [Test] 
        public void test_Iterator_0_1_2_3()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(0);
            bs.Include(1);
            bs.Include(2);
            bs.Include(3);

            RIterator<int> it = bs.TakeIterator();
            Assert.AreEqual(-1, it.Where);

            bool ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(0, it.Current);

            ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(1, it.Current);

            ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(2, it.Current);

            ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(3, it.Current);

            ok = 
                it.Next();
            Assert.IsFalse(ok);
            Assert.AreEqual(+1, it.Where);
        }


        [Test] 
        public void test_Iterator_0_63_64_127()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(0);
            bs.Include(63);
            bs.Include(64);
            bs.Include(127);

            RIterator<int> it = bs.TakeIterator();
            Assert.AreEqual(-1, it.Where);

            bool ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(0, it.Current);

            ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(63, it.Current);

            ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(64, it.Current);

            ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(127, it.Current);

            ok = 
                it.Next();
            Assert.IsFalse(ok);
            Assert.AreEqual(+1, it.Where);
        }


        [Test] 
        public void test_Iterator_1_201_202_700()
        {
            BitIntSet bs = new BitIntSet();

            bs.Include(1);
            bs.Include(201);
            bs.Include(202);
            bs.Include(700);

            RIterator<int> it = bs.TakeIterator();
            Assert.AreEqual(-1, it.Where);

            bool ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(1, it.Current);

            ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(201, it.Current);

            ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(202, it.Current);

            ok = 
                it.Next();
            Assert.IsTrue(ok);
            Assert.AreEqual(0, it.Where);
            Assert.AreEqual(700, it.Current);

            ok = 
                it.Next();
            Assert.IsFalse(ok);
            Assert.AreEqual(+1, it.Where);
        }





    }
}